//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#ifndef __LEVEL_GRAPH_YG_H__
#define __LEVEL_GRAPH_YG_H__

#include "def.h"
#include <wx/timer.h>

class CLevelGraph: public wxPanel
{
public:
	enum cGraphMode {cNormalGraphMode, cGrowGraphMode, cGrowBarGraphMode };
	enum cGraphLabelMode {cGraphLabelBottom, cGraphLabelRight };


								CLevelGraph(wxWindow* parent, cGraphMode cGraphMode=cNormalGraphMode, cGraphLabelMode cLabelMode=cGraphLabelRight, wxWindowID id=wxID_ANY);
	virtual 					~CLevelGraph();
	
	void						SetUpdateInterVal(unsigned int nUpdateTime);
	void						Reset();
	
	void						SetData(const wxString& strName, unsigned long long  lVal, time_t tEvent);
	void						SetDataEx(const wxString& strName, unsigned long long  lVal, time_t tEvent);

	void						SetTimePeriod(int nPer);
	int							GetTimePeriod()			{ return m_nTimePeriodSec; }

	void						SetColor(const wxString& strName, const wxColor& cl);
	wxColor						GetColor(const wxString& strName);

	void						SetZones(const ZONELIST& lstZone);

	void						SetMesureName(const wxString& str) { m_strMesureItemName=str; }
	unsigned long long			GetCurVal(const wxString& strName, bool& bEmpty);

	void						SetLabelMode(cGraphLabelMode cMode) { m_cLabelMode = cMode; }

	void						PairUp(struct CGraphSize* pGetSizezFrom) { m_pGetSizezFrom = pGetSizezFrom; }

protected:
	void						OnSize(wxSizeEvent& event);
	void						OnPaint(wxPaintEvent& event);
	void 						OnTimer(wxTimerEvent& event);
	
private:
	typedef std::map<wxString, wxColor> COLORMAP;

	typedef struct tagTRAFFICDATAENTRY
	{
		tagTRAFFICDATAENTRY():bCompressed(false), t(0), data(0)
		{
		}

		time_t		  		t;
		unsigned long long 	data;
		bool				bCompressed;
	}TRAFFICDATAENTRY;
	
	typedef std::list<TRAFFICDATAENTRY>		TRAFFICDATA;
	typedef std::map<wxString,TRAFFICDATA>	TRAFFICDATASTORAGE;


	void						AddData( TRAFFICDATA& data, unsigned long long lVal, time_t tEvent);
	unsigned long				GetMax( const TRAFFICDATA& data );
	//void						ConvertToBandwidth(TRAFFICDATA& data,	BANDWIDTHDATA& bndwdata);
	void						RemoveOldValues(TRAFFICDATA& data, time_t tExpired);
	
	
private:
	void						DrawGrid( class wxBufferedPaintDC& dc, unsigned int lDataMax, wxRect&  rectGraphArea);

	void						DrawLegend( class wxBufferedPaintDC& dc, int&  w, int&  h);
	void						DrawLegend(  wxBufferedPaintDC& dc, int x, int& w, int& h, bool bDraw);
	void						DrawLegendBottom(  wxBufferedPaintDC& dc, int x, int& w, int& h, bool bDraw);
	void						DrawLegendRight(  wxBufferedPaintDC& dc, int x, int y, int& w, int& h, int& wBox, bool bDraw);
	
	void						DrawGridLines(class wxBufferedPaintDC& dc, int x, int y, int w, int h);
	void						DrawTimeLabels(class wxBufferedPaintDC& dc, int x, int y, int w, int h, bool bSimple);
	void						DrawTraphicLabels(class wxBufferedPaintDC& dc, int x, int y, int w, int h, unsigned int lDataMax);
	void						DrawZones(class wxBufferedPaintDC& dc, int x, int y, int w, int h, unsigned int lDataMax, const wxRect&  rectGraphArea);
	void						DrawGraph(class wxBufferedPaintDC& dc, const TRAFFICDATA& data, const wxRect& rect, const wxColor& color);
	void						DrawDiffGraph(class wxBufferedPaintDC& dc, const TRAFFICDATA& data, const wxRect& rect, const wxColor& color);
	void						DrawDiffGraphBar(class wxBufferedPaintDC& dc, const TRAFFICDATA& data, const wxRect& rect, const wxColor& color);


	unsigned int				CalculateUpLevel(unsigned int lDataMax);
	void						CalculateBlockParams(int nLineLength, int* nBlocksMap,int nMapSize, int nBlockMinSize, int &nBlockSize, int& nBlockCount);

	wxString					FormatLabel( unsigned long lVal, unsigned long lMax);
	wxString					FormatTime(int nTimeBlockIndex, bool bSimple);
	unsigned long 				GetScale(unsigned long lMax);
	unsigned long				GetCurBandwidth(const TRAFFICDATA& data);
	void						CompressList( TRAFFICDATA& data );
	void						QuickCompress( TRAFFICDATA& data );
	void						VerifyData( TRAFFICDATA& data );
	const TRAFFICDATA&			GetData(const wxString& strName );
	unsigned long				GetMaxDataVal();
	bool						GetDistance(const TRAFFICDATAENTRY& entry1, const TRAFFICDATAENTRY& entry2, int& x, int& y);
	

	
private:
	wxTimer						m_timer;
	unsigned int 				m_nUpdateTime;
	
	TRAFFICDATASTORAGE			m_lstGraphData;
	
	int							m_nTrafBlockSize;
	int							m_nTrafBlockCount;

	int							m_nTimeBlockSize;
	int							m_nTimeBlockCount;
	unsigned int				m_lUpLevel;
	int							m_nTimePeriodSec;


	COLORMAP					m_mapColor;

	ZONELIST					m_lstZone;

	cGraphMode					m_cGraphMode;

	wxString					m_strMesureItemName;

	cGraphLabelMode				m_cLabelMode;

	struct CGraphSize*			m_pGetSizezFrom;
};

#endif /*__LEVEL_GRAPH_YG_H__*/
